package com.gusha.service;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.gusha.dao.SysProcessingDao;
import com.gusha.model.SysProcessing;
@Service
@Transactional
public class SysProcessingService {
	@Autowired
	private SysProcessingDao sysProcessingDao;

	public Page<SysProcessing> findAll(Pageable pageable, SysProcessing sysProcessing) {

		@SuppressWarnings("serial")
		Page<SysProcessing> result = sysProcessingDao.findAll(new Specification<SysProcessing>() {
			@Override
			public Predicate toPredicate(Root<SysProcessing> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
				List<Predicate> list = new ArrayList<Predicate>();
				if (sysProcessing.getId() != null) {
					list.add(cb.equal(root.get("id").as(BigInteger.class), sysProcessing.getId()));
				}
				if (sysProcessing.getProcessingName() != null) {
					list.add(cb.equal(root.get("processingName").as(String.class), sysProcessing.getProcessingName()));
				}
				if (sysProcessing.getProcessingImg() != null) {
					list.add(cb.equal(root.get("processingImg").as(String.class), sysProcessing.getProcessingImg()));
				}
				if (sysProcessing.getProcessingDesc() != null) {
					list.add(cb.equal(root.get("processingDesc").as(String.class), sysProcessing.getProcessingDesc()));
				}
				if (sysProcessing.getProcessingNum() != null) {
					list.add(cb.equal(root.get("processingNum").as(BigInteger.class), sysProcessing.getProcessingNum()));
				}
				if (sysProcessing.getProcessingPrice() != null) {
					list.add(cb.equal(root.get("processingPrice").as(BigDecimal.class),sysProcessing.getProcessingPrice()));
				}
				if (sysProcessing.getProcessingInfo() != null) {
					list.add(cb.equal(root.get("processingInfo").as(String.class), sysProcessing.getProcessingInfo()));
				}
				if (sysProcessing.getTradeNum() != null) {
					list.add(cb.equal(root.get("tradeNum").as(BigInteger.class), sysProcessing.getTradeNum()));
				}
				if (sysProcessing.getAddTime() != null) {
					list.add(cb.equal(root.get("addTime").as(Date.class), sysProcessing.getAddTime()));
				}
				if (sysProcessing.getIsDelete() != null) {
					list.add(cb.equal(root.get("isDelete").as(BigInteger.class), sysProcessing.getIsDelete()));
				}

				Predicate[] p = new Predicate[list.size()];
				return cb.and(list.toArray(p));
			}
		}, pageable);
		return result;
	}
}
